package com.example.demo.smartLamp.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.common.ApiResponse;
import com.example.demo.smartLamp.entity.Alarm;
import com.example.demo.smartLamp.entity.Environment;
import com.example.demo.smartLamp.entity.STM32;
import com.example.demo.smartLamp.entity.response.AlarmResponse;
import com.example.demo.smartLamp.entity.response.GetAllEnvironmentResponse;
import com.example.demo.smartLamp.entity.response.GpsResponse;
import com.example.demo.smartLamp.entity.response.STM32Response;
import com.example.demo.smartLamp.mapper.AlarmMapper;
import com.example.demo.smartLamp.mapper.GpsMapper;
import com.example.demo.smartLamp.mapper.STM32Mapper;
import com.example.demo.smartLamp.service.ISTM32Service;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class STM32ServiceImpl extends ServiceImpl<STM32Mapper, STM32> implements ISTM32Service {

    @Resource
    STM32Mapper stm32Mapper;

    @Resource
    private GpsMapper gpsMapper;

    @Resource
    private AlarmMapper alarmMapper;

    @Override
    public STM32Response GetSTM32Info() throws Exception {
        List<STM32> eList= new ArrayList<STM32>();
//        eList = stm32Mapper.selectList(Wrappers.lambdaQuery(STM32.class).orderByDesc(STM32::getStm32Time));
        eList = stm32Mapper.getClimateList();
        STM32 stm32 = eList.get(0);

        STM32Response stm32Response = new STM32Response();
        stm32Response.setStm32Pm10(stm32.getStm32Pm10());
        stm32Response.setStm32Pm25(stm32.getStm32Pm25());
        stm32Response.setStm32Time(stm32.getStm32Time());
        stm32Response.setStm32LightIntensity(stm32.getStm32LightIntensity());
        stm32Response.setStm32LightStatus(stm32.getStm32LightStatus());
        stm32Response.setStm32Battery(stm32.getStm32Battery());

        return stm32Response;
    }

    @Override
    public ApiResponse GetGps(int lightId) {
        GpsResponse gpsResponse = gpsMapper.getGps(lightId);
        if (gpsResponse == null){
            return  ApiResponse.error(201,"获取Gps数据不成功",null);
        }
        return  ApiResponse.success(200,"获取Gps数据成功",gpsResponse);
    }

    @Override
    public ApiResponse getSTM32alarm(int lampId) {
        //查询指定灯杆的报警信息
        List<Alarm> alarmList = alarmMapper.getAllLightAlarm(lampId);
        Alarm alarm = alarmList.get(0);
        return ApiResponse.success(200,"获取报警信息成功",alarm);
    }
}
